package com.ashleyleung.jetpack_b_station.player

import android.os.Bundle
import android.os.Looper
import com.ashleyleung.jetpack_b_station.SysApplication
import com.ashleyleung.jetpack_b_station.lifecycle.*
import com.ashleyleung.jetpack_b_station.player.domain.Music
import timber.log.Timber

/**
 * @author lxy
 * @name
 * 播放
 * 暂停
 * 上一首
 * 下一首
 * --------
 *插播放的状态:
 *-通知UI改变成播放状态
 * -通知UI进度的变化
 *
 * 上一首，下一百
 *-通知uI歌曲标题变化
 *-通知UI歌出封面变化
 *
 * 暂停音乐
 *更新UI状态为暂停
 * --------------------------------------------6-22----------
 * 数据驱动：
 *
 *当前播放的歌曲
 * 当前播放的状态
 *
 *
 * @class describe
 * @date 2022-06-21
 *
 */
class PlayerPresenter(owner: ILifeCycleOwner) : ViewLifeImplAbsLifecycle() {
    //class PlayerPresenter private constructor() {
    //改为单例
    private val playerModel by lazy { PlayerModel() }

//    private var currMusic: Music? = null


    override fun onCreateLc() {
        super.onCreateLc()
        Timber.d("onCreate PlayerPresenter")
        Timber.e("监听网络状态变化。。。")
    }

    private val player by lazy {
        MusicPlayer()
    }

    //    private var currentMusic: Music? = null   //数据驱动就不这样写了
    public var currentMusic = DataListenContainer<Music>()//去掉private 让数据可以监听到
    var livePlayState = LivePlayerState.instance

    //    private var currentPlayState = PlayState.NONE
    var currentPlayState =
        DataListenContainer<PlayState>()//   bug    DataListenContainer并没能保存数据  播放状态并没能保持  解决方法：DataListenContainer    setValue 加上这句   field = value//这句关键。前面
//
//    companion object {
//        val instance by lazy {
//            PlayerPresenter(owner)
//        }
//    }

    enum class PlayState {
        NONE, PLAYING, PAUSED, LOADING
    }

    private val callbacksList = arrayListOf<IPlayerCallback>() //数据驱动不需要了


    fun registerCallback(callback: IPlayerCallback) {
        if (!callbacksList.contains(callback)) {
            callbacksList.add(callback)
        }

    }

    fun unregisterCallback(callback: IPlayerCallback) {
        callbacksList.remove(callback)
    }

    /**
     * @name PlayerPresenter
     * @describe 根据状态控制音乐播放还是暂停  上一首  下一首
     * @author liangxy
     * @time 2022/6/21 15:51
     *
     */
    var x: Int = 0

    fun doPlayOrPause() {
        //   数据驱动不需要了
        /*       if (currMusic == null) {
              //获取一首歌曲
              currMusic = playerModel.getMusicById("xxx")
          }

          player.play(currMusic)



          dispatchTitleChange("当前播放的歌曲标题...${currMusic?.name}")
               dispatchCoverChange("当前播放的歌曲封面....${currMusic?.cover}")




               if (currentPlayState == PlayState.PLAYING) {
                   //暂停
                   dispatchPauseState()
               } else {
                   //开始播放音乐
                   dispatchPlayingState()
               }
       */
        if (currentMusic.value == null) {
            //获取一首歌曲
            currentMusic.value = playerModel.getMusicById("xxx")
        }else{
            currentMusic.value =playerModel.getMusicById("少女的祈祷")
        }

        player.play(currentMusic.value)


//            currentPlayState.value = PlayState.PLAYING
//bug状态永远保存不了
        Timber.e("当前状态 : %s", currentPlayState.value.toString())
        if (currentPlayState.value != PlayState.PLAYING) {

            currentPlayState.value = PlayState.PLAYING
//            livePlayState.postValue(PlayState.PLAYING)



            if (Looper.getMainLooper().thread === Thread.currentThread()) {
                livePlayState.value = PlayState.PLAYING//这种写法要先判断是否在主线程否则报错
            } else {
                SysApplication.handler.post {
                    livePlayState.value = PlayState.PLAYING//这种写法要先判断是否在主线程否则报错
                }
            }

            //如果用setValue要确保在主线程
            Timber.e(
                "当前状态 : %s , %s",
                currentPlayState.toString(),
                currentPlayState.value.toString()
            )
        } else {
            currentPlayState.value = PlayState.PAUSED
            livePlayState.postValue(PlayState.PAUSED)
        }


    }


    ////数据驱动不需要了
    /*   fun dispatchPauseState() {
           callbacksList.forEach {
               it.onPlayerPause()
               currentPlayState = PlayState.PAUSED

           }
       }

       fun dispatchPlayingState() {

           callbacksList.forEach {
               it.onPlaying()
               currentPlayState = PlayState.PLAYING

           }
       }*/


    fun playNext() {
        //播放下一首
        //1、拿到下一首歌曲-- >变更UI，包括标题和封面
        currentMusic.value = playerModel.getMusicById("下一首：火鸟")

//  数据驱动不需要了
        /*      dispatchTitleChange("切换到下一首，标题变了")
              dispatchCoverChange("切换到下一首，封面变了")
              //2、设置给播放器
              // 3、等待播放的回调通知
              currentPlayState = PlayState.PLAYING*/
        currentPlayState.value = PlayState.PLAYING

    }

    /*数据驱动不需要了*/
    fun dispatchCoverChange(cover: String) {
        callbacksList.forEach {
            it.onCoverChange(cover)
        }
    }

    fun dispatchTitleChange(title: String) {
        callbacksList.forEach {
            it.onTitleChange(title)
        }
    }

    fun playPre() {
        currentMusic.value = playerModel.getMusicById("上一首：白天鹅")
//     数据驱动不需要了
/*        dispatchTitleChange("切换到上一首，标题变了")
        dispatchCoverChange("切换到上一首，封面变了")
        currentPlayState = PlayState.PLAYING*/
        currentPlayState.value = PlayState.PLAYING
    }

    override fun onViewLifeStateChange(state: LifeState) {
        TODO("Not yet implemented")
    }


    override fun onStartLc() {
        Timber.e("开始监听网络状态变化。。。")
    }

    override fun onResumeLc() {
    }

    override fun onPauseLc() {
    }

    override fun onStopLc() {
        Timber.e("停止监听网络状态变化。。。")
    }

    override fun onDestroyLc() {
    }


}